import { isMpWeixin, isMpBaidu } from './uniPlatform'
/*
 * @Author: hyq
 * @LastEditors: hyq
 * @Description: uni api promise化
 * @Date: 2023-12-05 10:39:18
 */

import { nextTick } from 'vue'
// 路由
export function navigateTo(url: string) {
  return new Promise((resolve, reject) => {
    uni.navigateTo({
      url: url,
      success: () => {
        resolve(true)
      },
      fail: (e: any) => {
        console.log(e)
        reject(false)
      }
    })
  })
}

// 重定向
export function redirectTo(url: string) {
  return new Promise((resolve, reject) => {
    uni.redirectTo({
      url: url,
      success: () => {
        resolve(true)
      },
      fail: () => {
        reject(false)
      }
    })
  })
}

// 关闭所有，打开某个页面
export function reLaunch(url: string) {
  return new Promise((resolve, reject) => {
    uni.reLaunch({
      url: url,
      success: () => {
        resolve(true)
      },
      fail: () => {
        reject(false)
      }
    })
  })
}

export function setNavigationBarTitle(title: string) {
  uni.setNavigationBarTitle({
    title: title || '量子炬核ETC办理中心'
  })
}

// 切换tab
export function switchTab(url: string) {
  return new Promise((resolve, reject) => {
    uni.switchTab({
      url: url,
      success: () => {
        resolve(true)
      },
      fail: () => {
        reject(false)
      }
    })
  })
}

// 返回
export function navigateBack(delta = 1) {
  return new Promise((resolve, reject) => {
    uni.navigateBack({
      delta: delta,
      success: () => {
        resolve(true)
      },
      fail: () => {
        reject(false)
      }
    })
  })
}

// 弹框提示
export function uniToast(msg: string) {
  return new Promise(resolve => {
    uni.showToast({
      title: msg,
      icon: 'none',
      duration: 4000,
      success: () => {
        resolve(true)
      }
    })
  })
}

// loading
export function uniLoading(title: string) {
  uni.showLoading({ title })
}

// hide loading
export function uniHideLoading() {
  uni.hideLoading()
}

// 模态框
export function uniModal(options: {
  title?: string
  content?: string
  showCancel?: boolean
  cancelText?: string
  cancelColor?: string
  confirmText?: string
  confirmColor?: string
  editable?: boolean
  placeholderText?: string
  success?: (res: any) => void
  fail?: (res: any) => void
  complete?: () => void
}): Promise<boolean> {
  return new Promise((resolve, reject) => {
    uni.showModal({
      ...options,
      success: (res: { confirm: boolean; cancel: boolean }) => {
        resolve(res.confirm)
      },
      fail: () => {
        reject(false)
      }
    })
  })
}

// 复制
export function onCopy(value: string) {
  return new Promise((resolve, reject) => {
    uni.setClipboardData({
      data: value,
      success: () => {
        uniToast('复制成功')
        resolve(true)
      },
      fail: () => {
        uniToast('复制失败，请重试')
        reject()
      }
    })
  })
}

// 获取provider
export async function uniGetProvider() {
  return new Promise((resolve, reject) => {
    uni.getProvider({
      service: 'oauth',
      success: (res: any) => {
        resolve(res.provider[0])
      },
      fail: (err: any) => {
        uni.showToast({
          title: err.errMsg,
          icon: 'none'
        })
        reject('')
      }
    })
  })
}

// login
export async function uniLogin(): Promise<string> {
  const provider: any = await uniGetProvider()
  if (provider === '') {
    return Promise.reject('')
  }
  return new Promise((resolve, reject) => {
    uni.login({
      provider: provider,
      success: (res: any) => {
        resolve(res.code)
      },
      fail: (err: any) => {
        uni.showToast({
          title: err.errMsg,
          icon: 'none'
        })
        reject('')
      }
    })
  })
}

// 检测session
export async function uniCheckSession(): Promise<boolean> {
  return new Promise((resolve, reject) => {
    wx.checkSession({
      success() {
        resolve(true)
      },
      fail() {
        reject(false)
      }
    })
  })
}

// 拨打电话
export async function uniMakePhoneCall(phoneNumber: string) {
  return new Promise((resolve, reject) => {
    uni.makePhoneCall({
      phoneNumber: phoneNumber,
      success: () => {
        resolve(true)
      },
      fail: () => {
        reject(false)
      }
    })
  })
}

export async function uniPay(orderNo: string): Promise<boolean | string> {
  const params = {
    orderNo: orderNo,
    clientIp: '127.0.0.1',
    payChannel: 'WX_JSAPI_PAY'
  }

  // const res: any = await etcApi.payUsingPOST(params)
  const res: any = []
  return new Promise((resolve, reject) => {
    if (res.success) {
      if (res.amount == 0) {
        resolve('1000')
      } else {
        uni.requestPayment({
          provider: 'wxpay',
          orderInfo: '', // 订单数据
          timeStamp: res.payment.timeStamp,
          nonceStr: res.payment.nonceStr,
          package: 'prepay_id=' + res.payment.prepayId,
          signType: res.payment.signType,
          paySign: res.payment.paySign,
          success: () => {
            resolve(res.outTradeNo)
          },
          fail: () => {
            reject(false)
          }
        })
      }
    }
  })
}

export function nextTickMock(fn: () => void) {
  if (isMpWeixin) {
    wx.nextTick(fn)
  } else if (isMpBaidu) {
    swan.nextTick(fn)
  } else {
    nextTick(fn)
  }
}
